import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.utils import formataddr


def send_email(receivers, subject, content):
    sender = '543857523@qq.com'
    password = 'qqjjzkwhvwnybfci'  # 应该从环境变量或配置文件中获取
    try:
        # 创建 MIMEText 对象，指定邮件内容和 MIME 类型
        msg = MIMEText(content, 'html', 'utf-8')
        # 设置邮件头部信息
        msg['From'] = formataddr(('工资条', sender))
        msg['Subject'] = Header(subject, 'utf-8')  # 使用 Header 以确保正确编码

        # 连接到 SMTP 服务器
        server = smtplib.SMTP_SSL('smtp.qq.com', 465)
        server.login(sender, password)

        # 设置邮件接收者，这里在循环外部设置
        msg['To'] = Header(''.join(receivers), 'utf-8')

        # 发送邮件
        server.sendmail(sender, receivers, msg.as_string())

        # 关闭服务器连接
        server.quit()
        return True
    except Exception as e:
        print('邮件发送失败，错误信息：', str(e))
        return False


email = "1927705375@qq.com"
content = """
<div style="background-color: white;width: 50%;border: 5px solid #F6F6FC;border-radius: 8px;margin: auto">
<div style="text-align: center; margin-top: 5%; font-size: 25px">通知</div>
 尊敬的***，************************************************************************************************
</div>
"""


send_email(email, "工资条", content)
